package com.anyou.mall.dri.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.anyou.common.constant.RedisKeyConstant;
import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.exception.GlobalException;
import com.anyou.common.utils.DateUtils;
import com.anyou.common.utils.uuid.RandomKit;
import com.anyou.mall.dri.domain.Driver;
import com.anyou.mall.dri.domain.IndividualAudit;
import com.anyou.mall.dri.mapper.DriverMapper;
import com.anyou.mall.dri.mapper.IndividualAuditMapper;
import com.anyou.mall.dri.param.IsPassIndividualAuditParam;
import com.anyou.mall.dri.service.IDriverService;
import com.anyou.mall.dri.service.IIndividualAuditService;
import com.anyou.mall.platform.domain.PlaPlatformSettings;
import com.anyou.mall.platform.dto.BondDTO;
import com.anyou.mall.platform.mapper.PlaPlatformSettingsMapper;
import com.anyou.mall.platform.service.IPlaPlatformSettingsService;
import com.anyou.mall.use.domain.UserAuditNotice;
import com.anyou.mall.use.domain.UserNotice;
import com.anyou.mall.use.service.IUserAuditNoticeService;
import com.anyou.mall.use.service.IUserNoticeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 司机加盟个人申请信息表Service业务层处理
 *
 * @author gaochaohao
 * @date 2022-03-22
 */
@Service
public class IndividualAuditServiceImpl extends ServiceImpl<IndividualAuditMapper,IndividualAudit> implements IIndividualAuditService
{
    @Autowired
    private IndividualAuditMapper individualAuditMapper;

    @Autowired
    private IIndividualAuditService individualAuditService;

    @Autowired
    private DriverMapper driverMapper;

    @Autowired
    private IDriverService driverService;

    @Autowired
    private IPlaPlatformSettingsService plaPlatformSettingsService;

    @Autowired
    private IUserAuditNoticeService userAuditNoticeService;

    @Autowired
    private IUserNoticeService userNoticeService;

    @Autowired
    private PlaPlatformSettingsMapper plaPlatformSettingsMapper;


    /**
     * 查询司机加盟个人申请信息表
     *
     * @param individualAuditId 司机加盟个人申请信息表主键
     * @return 司机加盟个人申请信息表
     */
    @Override
    public IndividualAudit selectIndividualAuditByIndividualAuditId(Long individualAuditId)
    {
        return individualAuditMapper.selectIndividualAuditByIndividualAuditId(individualAuditId);
    }

    /**
     * 查询司机加盟个人申请信息表列表
     *
     * @param individualAudit 司机加盟个人申请信息表
     * @return 司机加盟个人申请信息表
     */
    @Override
    public List<IndividualAudit> selectIndividualAuditList(IndividualAudit individualAudit)
    {
        return individualAuditMapper.selectIndividualAuditList(individualAudit);
    }

    /**
     * 新增司机加盟个人申请信息表
     *
     * @param individualAudit 司机加盟个人申请信息表
     * @return 结果
     */
    @Override
    public int insertIndividualAudit(IndividualAudit individualAudit)
    {
        individualAudit.setCreateTime(DateUtils.getNowDate());
        return individualAuditMapper.insertIndividualAudit(individualAudit);
    }

    /**
     * 修改司机加盟个人申请信息表
     *
     * @param individualAudit 司机加盟个人申请信息表
     * @return 结果
     */
    @Override
    public int updateIndividualAudit(IndividualAudit individualAudit)
    {
        individualAudit.setUpdateTime(DateUtils.getNowDate());
        return individualAuditMapper.updateIndividualAudit(individualAudit);
    }

    /**
     * 批量删除司机加盟个人申请信息表
     *
     * @param individualAuditIds 需要删除的司机加盟个人申请信息表主键
     * @return 结果
     */
    @Override
    public int deleteIndividualAuditByIndividualAuditIds(Long[] individualAuditIds)
    {
        return individualAuditMapper.deleteIndividualAuditByIndividualAuditIds(individualAuditIds);
    }

    /**
     * 删除司机加盟个人申请信息表信息
     *
     * @param individualAuditId 司机加盟个人申请信息表主键
     * @return 结果
     */
    @Override
    public int deleteIndividualAuditByIndividualAuditId(Long individualAuditId)
    {
        return individualAuditMapper.deleteIndividualAuditByIndividualAuditId(individualAuditId);
    }

    @Override
    public ApiResult isPassIndividualAudit(IsPassIndividualAuditParam isPassIndividualAuditParam) {
        Driver driver = new Driver();
        IndividualAudit individualAudit = individualAuditService.getOne(new LambdaQueryWrapper<IndividualAudit>().eq(IndividualAudit::getIndividualAuditId, isPassIndividualAuditParam.getIndividualAuditId()));
        if (individualAudit != null) {
            if ("1".equals(isPassIndividualAuditParam.getAuditStatus()) && !"1".equals(individualAudit.getAuditStatus())) {

                //生成个人审核入住保证金订单号
                String orderId = RandomKit.genOrderId();
                individualAudit.setIndividualBandOrderNo("ind" + orderId);
                individualAudit.setAuditStatus("1");
                BeanUtils.copyProperties(individualAudit,driver);
                //设置司机联系地址
                driver.setProvince(individualAudit.getContactProvince());
                driver.setCity(individualAudit.getContactCity());
                driver.setArea(individualAudit.getContactArea());
                driver.setAddr(individualAudit.getContactAddress());
                Driver one = driverMapper.selectOne(new LambdaQueryWrapper<Driver>()
                        .eq(Driver::getIndividualAuditPhone, driver.getIndividualAuditPhone()));
                if(one!=null){
                    driver.setDriverId(one.getDriverId());
                    driverMapper.updateById(driver);
                    int i = individualAuditMapper.updateIndividualAudit(individualAudit);
                }
                //将通过的审核信息插入到司机表中
                driverMapper.insertDriver(driver);
                int i = individualAuditMapper.updateIndividualAudit(individualAudit);
                if (i > 0) {
                    //查看是否需要缴纳保证金
                    Boolean payBaod = plaPlatformSettingsService.isPayBaod(3);
                    if (!payBaod) {
                        //修改司机审核表中的状态
                        individualAudit.setPayBondTime(new Date());
                        individualAudit.setBondStatus("1");
                        individualAuditService.updateById(individualAudit);
                        //将数据插入到司机信息表中
                        Driver driver1 = driverService.getOne(new QueryWrapper<Driver>()
                                .eq("individual_audit_phone", individualAudit.getIndividualAuditPhone()));
                        if (driver1 == null) {
                            Driver driver2 = new Driver();
                            BeanUtils.copyProperties(individualAudit,driver2);
                            driver2.setProvince(individualAudit.getContactProvince());
                            driver2.setCity(individualAudit.getContactCity());
                            driver2.setArea(individualAudit.getContactArea());
                            driver2.setAddr(individualAudit.getContactAddress());
                            driverService.save(driver2);
                        }
                    }
                }
                return ApiResult.success();
            } else if ("2".equals(isPassIndividualAuditParam.getAuditStatus())) {

                IndividualAudit individualAudit1 = individualAuditService.getOne(new LambdaQueryWrapper<IndividualAudit>()
                        .eq(IndividualAudit::getIndividualAuditId, isPassIndividualAuditParam.getIndividualAuditId()));
                individualAudit1.setAuditStatus("2");
                individualAudit1.setFailReason(isPassIndividualAuditParam.getFailReason());
                return ApiResult.success(individualAuditService.updateById(individualAudit1));
            } else {
                return ApiResult.error("审核失败");
            }
        } else {
            return ApiResult.error("审核失败");
        }
    }

    @Override
    public ApiResult allPassIndividualAudit(Long[] individualAuditIds) {
        Driver driver = new Driver();
        if (individualAuditIds == null) {
            return ApiResult.error("请选择要通过的信息");
        }
        for (Long individualAuditId : individualAuditIds) {
            IndividualAudit individualAudit = individualAuditMapper.selectIndividualAuditByIndividualAuditId(individualAuditId);
            if (individualAudit != null && !"1".equals(individualAudit.getAuditStatus())) {

                //生成个人审核入住保证金订单号
                String orderId = RandomKit.genOrderId();
                individualAudit.setIndividualBandOrderNo("ind" + orderId);
                individualAudit.setAuditStatus("1");
                BeanUtils.copyProperties(individualAudit,driver);
                //通过审核的司机信息添加到司机表中
                driverMapper.insertDriver(driver);
                int i = individualAuditMapper.updateIndividualAudit(individualAudit);
                if (i > 0) {
                    //查看是否需要缴纳保证金
                    Boolean payBaod = plaPlatformSettingsService.isPayBaod(3);
                    if (!payBaod) {
                        //修改司机审核表中的状态
                        individualAudit.setPayBondTime(new Date());
                        individualAudit.setBondStatus("1");
                        individualAuditService.updateById(individualAudit);
                        //将数据插入到司机信息表中
                        Driver driver1 = driverService.getOne(new QueryWrapper<Driver>()
                                .eq("individual_audit_phone", individualAudit.getIndividualAuditPhone()));
                        if (driver1 == null) {
                            Driver driver2 = new Driver();
                            BeanUtils.copyProperties(individualAudit,driver2);
                            driver2.setProvince(individualAudit.getContactProvince());
                            driver2.setCity(individualAudit.getContactCity());
                            driver2.setArea(individualAudit.getContactArea());
                            driver2.setAddr(individualAudit.getContactAddress());
                            driverService.save(driver2);
                        }
                    }
                }
            }
        }
        return ApiResult.success();
    }


}
